<html>

<head>
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
    <meta http-equiv="Pragma" content="no-cache" />
    <meta http-equiv="Expires" content="0" />
</head>
<script type="text/javascript">

    //获得func
    //wechat. 主页 http://story.aidoor.net/wechat/
    //mydevice. 我的 http://story.aidoor.net/mydevice/
    //airkiss. 联网 http://story.aidoor.net/airkiss/
    //wechat. 绑定 http://story.aidoor.net/wechat/
    //逻辑
    //1. 根据不同的func值 写重定义的地址
    //2. 查询cookie值，如果不为空，可以直接定向到目标目录
    //3. 如果为空，定向到微信的页面去

    function sendHttp(url, callback) {
        var xhr = new XMLHttpRequest();
        xhr.overrideMimeType("application/json");

        var timer = setTimeout(function () {
            this.timedout = true;
            xhr.abort();
        }, 3000);

        xhr.open("GET", url, true);  // 有风险
        xhr.onreadystatechange = function () {
            // console.log(xhr)
            if (xhr.readyState !== 4) { return; }
            if (this.timedout) { return; }
            clearTimeout(timer);
            if (xhr.readyState === 4 && xhr.status === 200) {
                callback(xhr.responseText);
            }
        }
        xhr.send(null);
    }


	/* 
	* 设置Cookie的通用函数，其中name是必须的参数。其它为可选，故用条件语句判断。 
	* 在设置Cookie时若不设置过期时间则该Cookie为临时的，仅当此次会话可用 
	*/
    function setcookie(name, value, expires, path, domain, secure) {
        var curcookie = name + "=" + encodeURI(value)
            + ((expires) ? ";expires=" + expires.toGMTString() : "")
            + ((path) ? ";path=" + path : "")
            + ((domain) ? ";domain=" + domain : "")
            + ((secure) ? ";secure" : "");
        document.cookie = curcookie;
    }

    // Cookie的写入 
    function saveCookie(name, value) {
        var now = new Date();
        var time = 24 * 60 * 60 * 1000;
        now.setDate(now.getDate() + time);
        setcookie(name, value, now, '/');
    }

	/* 
	* 读取特定Cookie的通用函数 
	*/
    function getCookie(name) {
        if (document.cookie.length > 0) {
            var start = document.cookie.indexOf(name + "=");
            if (start !== -1) {
                start = start + name.length + 1;
                var end = document.cookie.indexOf(";", start);
                if (end === -1) {
                    end = document.cookie.length;
                }
                return decodeURI(document.cookie.substring(start, end));
            }
        }
        return "";
    }

    //得到url参数
    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r !== undefined && r != null) {
            console.log(unescape(r[2]));
            return unescape(r[2]);
        }
        return null;
    }

    function turnTo() {
        var func = GetQueryString('func');
        // 菜单按钮进入的界面都加入对应的版本
        if (func === 'wechat') {
            func = 'story';
        }
        if (func.indexOf('story')>=0 || func.indexOf('mydevice')>=0) {
            func += '_1';
        }
        var wechat_id = GetQueryString('wechat_id');
        var ticket = GetQueryString('ticket');

        var openid = getCookie('openid_' + wechat_id);
        saveCookie('wechat_id', wechat_id);

        var CLOUD_ADDR = window.location.protocol + '//' + window.location.host;
        if (openid && openid !='undefined' && openid.length > 0) {
            var url = CLOUD_ADDR + '/' + func;
            if (func.indexOf('share_device') >= 0) {
                url += '/:' + ticket;
            }
            window.location.href = url;
        } else {
            var url = CLOUD_ADDR + '/' + func + '?wechat_id=' + wechat_id;
            if (func.indexOf('share_device') >= 0) {
                url += '&ticket=' + ticket;
            }
            sendHttp(CLOUD_ADDR + "/web_api/get_public_platform?wechat_id=" + wechat_id, function (text) {
                var json = JSON.parse(text);
                if (json.errcode === 0) {
                    var appid = json.data.app_id;
                    var redirect_url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appid + '&redirect_uri=' + CLOUD_ADDR + '/getopenid?url=' + url + '&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect';
                    window.location.href = redirect_url;
                }
            });
        }
    }
    turnTo();

</script>

</html>